package com.chinacoal.hr.levelimpl.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.chinacoal.hr.levelapi.client.AttdanceSummaryClient;
import com.chinacoal.hr.levelapi.client.response.Result;
import com.chinacoal.hr.levelapi.client.response.ResultGenerator;
import com.chinacoal.hr.levelapi.entity.AttendanceSummary;
import com.chinacoal.hr.levelapi.entity.SummaryCalcVO;
import com.chinacoal.hr.levelimpl.service.AttendanceSummaryService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: YunTao.Li
 * @create: 2019-11-26 15:02
 * @description:
 **/

@RestController
public class AttdanceSummaryController implements AttdanceSummaryClient {

    @Autowired
    private AttendanceSummaryService attendanceSummaryService;

    /**
     * @param page      :
     * @param limit     :
     * @param orgCode   :
     * @param deptId    :
     * @param yearmonth :
     * @param empCode   :
     * @return : com.chinacoal.hr.levelapi.response.Result
     * @author : YunTao.Li
     * @date : 2019/11/26 2019/11/26
     */

    public Result list(Long page,
                       Long limit,
                       String orgCode,
                       String deptId,
                       String deptName,
                       String yearmonth,
                       String empCode) {
        // 构建查询条件
        Map<String, Object> condition = new HashMap<String, Object>();
        condition.put("page", page);
        condition.put("limit", limit);
        condition.put("orgCode", orgCode);
        condition.put("deptid", deptId);
        condition.put("yrmonth", yearmonth);
        condition.put("empCode", empCode);
        condition.put("deptName", deptName);

        IPage<AttendanceSummary> recordList = attendanceSummaryService.getAttendanceSummaryList(condition);
        return ResultGenerator.genOkResult(recordList);
    }

    /**
     * 考勤统计
     *
     * @return : com.chinacoal.hr.levelimpl.response.Result
     * @author : YunTao.Li
     * @date : 2020/7/24 2020/7/24
     */
    @ResponseBody
    @PostMapping("/summary/calc")
    @ApiOperation(value = "考勤统计")
    public Result calc(@RequestBody SummaryCalcVO calcVO) {
//        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
//        String yrmonth = format.format(new Date());
        String yrmonth = calcVO.getYearmonth();
        System.out.println("考勤统计 yrmonth：" + yrmonth);

        if (StringUtils.isBlank(yrmonth)) {
            return ResultGenerator.genFailedResult("统计年月为空");
        }

        try {
            this.attendanceSummaryService.attendanceSummaryRolling(yrmonth);
            return ResultGenerator.genOkResult();
        } catch (Throwable e) {
            e.printStackTrace();
            return ResultGenerator.genFailedResult(e.getMessage());
        }
    }
}
